package android.slc.user.ui.dept.activity

import android.os.Bundle
import android.slc.appbase.ui.activity.base.AppMvvmBaseToolBarActivity
import android.slc.popup.SlcPopup2
import android.slc.user.R
import android.slc.user.databinding.UserActivityDeptListBinding
import android.slc.user.ui.dept.fragment.DeptTreeListMultipleNavFragment
import android.slc.user.ui.dept.vm.DeptMultipleShelfVm
import androidx.appcompat.app.AlertDialog
import com.blankj.utilcode.util.FragmentUtils

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 3:32
 *
 */
class DeptListMultipleActivity :
    AppMvvmBaseToolBarActivity<UserActivityDeptListBinding, DeptMultipleShelfVm>() {
    override fun setContentView(): Any {
        return R.layout.user_activity_dept_list
    }

    override fun onBindViewBefore() {
        super.onBindViewBefore()
        viewModel.initSelectData(intent.extras)
    }

    override fun onBindView(savedInstanceState: Bundle?) {
        super.onBindView(savedInstanceState)
        mSlcToolBarDelegate.setTitle(getTitleByIntent(R.string.user_label_dept_list))
        mSlcToolbar.inflateMenu(R.menu.user_menu_user_info_select_mutliple)
        mSlcToolBarDelegate.setOnMenuItemClickListener {
            this.viewModel.treeMultipleShelfVmBox.complete()
            onBackPressed()
            return@setOnMenuItemClickListener false
        }


        FragmentUtils.add(supportFragmentManager, DeptTreeListMultipleNavFragment().apply {
            arguments = intent.extras
        }, R.id.developersContentView)
    }

    override fun onBindViewLater() {
        super.onBindViewLater()
        this.viewModel.initVm()
    }

    override fun onBackPressed() {
        if (this.viewModel.treeMultipleShelfVmBox.isSelectChange()) {
            showPromptSave()
        } else {
            super.onBackPressed()
        }
    }

    private fun showPromptSave() {
        SlcPopup2.createAlertDialogOperate().show(
            AlertDialog.Builder(this)
                .setTitle(R.string.label_prompt)
                .setMessage(R.string.label_select_undone_prompt)
                .setPositiveButton(R.string.action_complete) { _, _ ->
                    this.viewModel.treeMultipleShelfVmBox.complete()
                    onBackPressed()
                }
                .setNegativeButton(R.string.action_cancel) { _, _ ->

                }
                .setNeutralButton(R.string.action_exit_without_save) { dialog, _ ->
                    dialog.dismiss()
                    this.viewModel.treeMultipleShelfVmBox.handlerCompleteSelectChange()
                    onBackPressed()
                }
                .create()
        )
    }
}